package com.situ.crm2026.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.crm2026.dao.CoachMapper;
import com.situ.crm2026.model.Coach;
import com.situ.crm2026.model.dto.CoachDto;
import com.situ.crm2026.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "coaches")
public class CoachServiceImpl implements CoachService {
    private CoachMapper coachMapper;
    @Autowired
    public void setCoachMapper(CoachMapper coachMapper) {
        this.coachMapper = coachMapper;
    }

    @Override
    @Cacheable(key = "'coach-page-' + #page.current + '-' + #page.size + '-' + (#csb?.name ?: 'all') + '-' + (#csb?.phone ?: 'all')")
    public Page<Coach> findAll(Page<Coach> page, Coach csb) {
        return coachMapper.findAll(page, csb);
    }

    //删除教练
    @CacheEvict(allEntries = true) // 删除操作时清空缓存
    @Override
    public int deleteByIds(List<Integer> ids) {
        return coachMapper.deleteByIds(ids);
    }
    @CacheEvict(allEntries = true) // 新增操作时清空缓存
    @Override
    public boolean save(Coach coach) {
        return coachMapper.insert(coach)==1;
    }
    @CacheEvict(allEntries = true) // 更新操作时清空缓存
    @Override
    public boolean update(Coach coach) {
        return coachMapper.updateById( coach)==1;
    }

    @Override
    public List<CoachDto> findByCoachId(Integer id) {
        return coachMapper.findByCoachId(id);
    }

    @Override
    public List<CoachDto> findByPrivateId(Integer id) {
        return coachMapper.findByPrivateId(id);
    }
}
